package org.example.config;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"org.example.mapper", "org.example.mone.mapper"})
@PropertySources({
        @PropertySource("classpath:db.ini") // 默认属性源
})
public class AppConfig {


    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory getSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource, @Qualifier("databaseIdProvider") DatabaseIdProvider databaseIdProvider) throws Exception {
        DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory();
        defaultSqlSessionFactory.setMybatisConfigLocation("classpath:mybatis-config.xml");
        defaultSqlSessionFactory.setMapperLocations("classpath*:/**/*Mapper.xml,classpath:**/*Mapper.xml");
        defaultSqlSessionFactory.setTypeAliasesPackage("org.example.domain,org.example.mone.model");
        defaultSqlSessionFactory.setTypeHandlersPackage("org.example.typehandler");
        return defaultSqlSessionFactory.getSessionFactory(dataSource, databaseIdProvider);
    }

}
